home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / rbsetnv1.zip / INCR.C < prev    next >
Text File  |  1990-10-19  |  2KB  |  101 lines

  1. /*
  2.  * incr.c - increment a numeric environment variable
  3.  *
  4.  * Author:        R. Brittain    4/11/90
  5.  *
  6.  * Syntax:
  7.  *        incr variable
  8.  *
  9.  *                    This code placed in the public domain
  10.  *
  11.  */
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <dos.h>
  15. #include <stdlib.h>
  16. #include <io.h>
  17.  
  18. /* size minimization measures */
  19. setenvp() {}
  20. void exit(int status) { _exit(status); }
  21.  
  22. #define MEMCHECK(x)        if ((x) == NULL) fatal("Out of memory",1)
  23.  
  24. #ifdef UNIXCOMPAT
  25.  #define strupr(x)            (x)        /* disable upper-casing env-vars */
  26. #endif
  27.  
  28. /* prototypes */
  29.  
  30. int      get_env_index(char *var, char **env_var);
  31. int       getenvseg(unsigned *envseg);
  32. unsigned get_env_var(unsigned env_addr, char ***env_var, int *count);
  33. void      put_env_var(unsigned env_addr, unsigned seglen, char **env_var);
  34.  
  35. void      fatal(char *msg, int status);
  36. char      *concat(char *s1, char *s2);
  37.  
  38. main(int argc, char **argv)
  39. {
  40.     char usage[] = "usage: incr variable \n";
  41.     unsigned env_addr, seglen;
  42.     int index, count;
  43.     char value[10], **env_var, *p;
  44.  
  45.     if (argc == 1)
  46.         fatal(usage,1);
  47.  
  48.     if ( !getenvseg(&env_addr))
  49.         fatal ("Cannot locate environment\n",2);
  50.  
  51.     /* convert variable to upper case for compatibility */
  52.     strupr(argv[1]);
  53.  
  54.     seglen = get_env_var( env_addr, &env_var, &count );
  55.     index  = get_env_index( argv[1], env_var );
  56.     if (index == count)
  57.         fatal (concat(argv[1]," not in environment "),1);
  58.  
  59.     /* increment the value of the variable */
  60.     p = strchr(env_var[index],'=')+1 ;
  61.     if (strspn(p,"-+0123456789") != strlen(p))
  62.         fatal (concat(argv[1]," is non-numeric - cannot increment"),1);
  63.  
  64.     /*
  65.      * change only the next line to decrement, or do whatever else you
  66.      * want with the numeric value
  67.      */
  68.     itoa(atoi(p) + 1, value, 10);
  69.  
  70.     *(strchr(env_var[index],'=')+1) = '\0';
  71.     env_var[index] = concat(env_var[index],value);
  72.     put_env_var(env_addr, seglen, env_var);
  73.     return(0);
  74. }
  75.  
  76.  
  77. char *concat(s1, s2)
  78. char *s1, *s2;
  79. {
  80. /*
  81.  * return the concatenation of s1 and s2 in malloced memory
  82.  */
  83.     char *p;
  84.     p = malloc(strlen(s1)+strlen(s2)+2);
  85.     if (p == (char *)NULL) {
  86.         fatal ("\nOut of memory\n",1);
  87.     } else {
  88.         strcpy(p,s1);
  89.         strcat(p,s2);
  90.     }
  91.     return(p);
  92. }
  93.  
  94. void fatal(char *msg, int status)
  95. {
  96.     write(2,msg,strlen(msg));
  97.     exit(status);
  98. }
  99.  
  100. #include "envfuncs.c"
  101.